
#include <BMap>
#include <BFont>
#include "member_BShortcut.h"

using namespace BWE;

Key BWE::StrToKey(const char* word)
{
	static BMap<BString, Key> app_strKeyMap;
	if (app_strKeyMap.empty())
	{
		app_strKeyMap.reserve(Key_Max);
		app_strKeyMap["alt"] = Key_Alt;
		app_strKeyMap["lalt"] = Key_LAlt;
		app_strKeyMap["ralt"] = Key_RAlt;

		app_strKeyMap["ctrl"] = Key_Ctrl;
		app_strKeyMap["lctrl"] = Key_LCtrl;
		app_strKeyMap["rctrl"] = Key_RCtrl;

		app_strKeyMap["shift"] = Key_Shift;
		app_strKeyMap["lshift"] = Key_LShift;
		app_strKeyMap["rshift"] = Key_RShift;

		app_strKeyMap["a"] = Key_A;
		app_strKeyMap["b"] = Key_B;
		app_strKeyMap["c"] = Key_C;
		app_strKeyMap["d"] = Key_D;
		app_strKeyMap["e"] = Key_E;
		app_strKeyMap["f"] = Key_F;
		app_strKeyMap["g"] = Key_G;
		app_strKeyMap["h"] = Key_H;
		app_strKeyMap["i"] = Key_I;
		app_strKeyMap["j"] = Key_J;
		app_strKeyMap["k"] = Key_K;
		app_strKeyMap["l"] = Key_L;
		app_strKeyMap["m"] = Key_M;
		app_strKeyMap["n"] = Key_N;
		app_strKeyMap["o"] = Key_O;
		app_strKeyMap["p"] = Key_P;
		app_strKeyMap["q"] = Key_Q;
		app_strKeyMap["r"] = Key_R;
		app_strKeyMap["s"] = Key_S;
		app_strKeyMap["t"] = Key_T;
		app_strKeyMap["u"] = Key_U;
		app_strKeyMap["v"] = Key_V;
		app_strKeyMap["w"] = Key_W;
		app_strKeyMap["x"] = Key_X;
		app_strKeyMap["y"] = Key_Y;
		app_strKeyMap["z"] = Key_Z;

		app_strKeyMap["1"] = Key_1;
		app_strKeyMap["2"] = Key_2;
		app_strKeyMap["3"] = Key_3;
		app_strKeyMap["4"] = Key_4;
		app_strKeyMap["5"] = Key_5;
		app_strKeyMap["6"] = Key_6;
		app_strKeyMap["7"] = Key_7;
		app_strKeyMap["8"] = Key_8;
		app_strKeyMap["9"] = Key_9;
		app_strKeyMap["0"] = Key_0;

		app_strKeyMap["num1"] = Key_Num1;
		app_strKeyMap["num2"] = Key_Num2;
		app_strKeyMap["num3"] = Key_Num3;
		app_strKeyMap["num4"] = Key_Num4;
		app_strKeyMap["num5"] = Key_Num5;
		app_strKeyMap["num6"] = Key_Num6;
		app_strKeyMap["num7"] = Key_Num7;
		app_strKeyMap["num8"] = Key_Num8;
		app_strKeyMap["num9"] = Key_Num9;
		app_strKeyMap["num0"] = Key_Num0;

		app_strKeyMap["f1"] = Key_F1;
		app_strKeyMap["f2"] = Key_F2;
		app_strKeyMap["f3"] = Key_F3;
		app_strKeyMap["f4"] = Key_F4;
		app_strKeyMap["f5"] = Key_F5;
		app_strKeyMap["f6"] = Key_F6;
		app_strKeyMap["f7"] = Key_F7;
		app_strKeyMap["f8"] = Key_F8;
		app_strKeyMap["f9"] = Key_F9;
		app_strKeyMap["f10"] = Key_F10;
		app_strKeyMap["f11"] = Key_F11;
		app_strKeyMap["f12"] = Key_F12;

		app_strKeyMap["tab"] = Key_Tab;
		app_strKeyMap["space"] = Key_Space;
		app_strKeyMap["del"] = Key_Delete;
		app_strKeyMap["delete"] = Key_Delete;
		app_strKeyMap["back"] = Key_Back;
		app_strKeyMap["enter"] = Key_Enter;

	}
	return app_strKeyMap(word);
}
const char* BWE::KeyToStr(Key key)
{
	static BMap<Key, const char*> app_keyStrMap;
	if (app_keyStrMap.empty())
	{
		app_keyStrMap.reserve(Key_Max);
		app_keyStrMap[Key_Alt] = "alt";
		app_keyStrMap[Key_LAlt] = "lalt";
		app_keyStrMap[Key_RAlt] = "ralt";

		app_keyStrMap[Key_Ctrl] = "ctrl";
		app_keyStrMap[Key_LCtrl] = "lctrl";
		app_keyStrMap[Key_RCtrl] = "rctrl";

		app_keyStrMap[Key_Shift] = "shift";
		app_keyStrMap[Key_LShift] = "lshift";
		app_keyStrMap[Key_RShift] = "rshift";

		app_keyStrMap[Key_A] = "a";
		app_keyStrMap[Key_B] = "b";
		app_keyStrMap[Key_C] = "c";
		app_keyStrMap[Key_D] = "d";
		app_keyStrMap[Key_E] = "e";
		app_keyStrMap[Key_F] = "f";
		app_keyStrMap[Key_G] = "g";
		app_keyStrMap[Key_H] = "h";
		app_keyStrMap[Key_I] = "i";
		app_keyStrMap[Key_J] = "j";
		app_keyStrMap[Key_K] = "k";
		app_keyStrMap[Key_L] = "l";
		app_keyStrMap[Key_M] = "m";
		app_keyStrMap[Key_N] = "n";
		app_keyStrMap[Key_O] = "o";
		app_keyStrMap[Key_P] = "p";
		app_keyStrMap[Key_Q] = "q";
		app_keyStrMap[Key_R] = "r";
		app_keyStrMap[Key_S] = "s";
		app_keyStrMap[Key_T] = "t";
		app_keyStrMap[Key_U] = "u";
		app_keyStrMap[Key_V] = "v";
		app_keyStrMap[Key_W] = "w";
		app_keyStrMap[Key_X] = "x";
		app_keyStrMap[Key_Y] = "y";
		app_keyStrMap[Key_Z] = "z";

		app_keyStrMap[Key_1] = "1";
		app_keyStrMap[Key_2] = "2";
		app_keyStrMap[Key_3] = "3";
		app_keyStrMap[Key_4] = "4";
		app_keyStrMap[Key_5] = "5";
		app_keyStrMap[Key_6] = "6";
		app_keyStrMap[Key_7] = "7";
		app_keyStrMap[Key_8] = "8";
		app_keyStrMap[Key_9] = "9";
		app_keyStrMap[Key_0] = "0";

		app_keyStrMap[Key_Num1] = "num1";
		app_keyStrMap[Key_Num2] = "num2";
		app_keyStrMap[Key_Num3] = "num3";
		app_keyStrMap[Key_Num4] = "num4";
		app_keyStrMap[Key_Num5] = "num5";
		app_keyStrMap[Key_Num6] = "num6";
		app_keyStrMap[Key_Num7] = "num7";
		app_keyStrMap[Key_Num8] = "num8";
		app_keyStrMap[Key_Num9] = "num9";
		app_keyStrMap[Key_Num0] = "num0";

		app_keyStrMap[Key_F1] = "f1";
		app_keyStrMap[Key_F2] = "f2";
		app_keyStrMap[Key_F3] = "f3";
		app_keyStrMap[Key_F4] = "f4";
		app_keyStrMap[Key_F5] = "f5";
		app_keyStrMap[Key_F6] = "f6";
		app_keyStrMap[Key_F7] = "f7";
		app_keyStrMap[Key_F8] = "f8";
		app_keyStrMap[Key_F9] = "f9";
		app_keyStrMap[Key_F10] = "f10";
		app_keyStrMap[Key_F11] = "f11";
		app_keyStrMap[Key_F12] = "f12";

		app_keyStrMap[Key_Tab] = "tab";
		app_keyStrMap[Key_Space] = "space";
		app_keyStrMap[Key_Delete] = "del";
		app_keyStrMap[Key_Back] = "back";

	}
	return app_keyStrMap(key);
}

member_BShortcut::member_BShortcut(BShortcut* shortcut)
{
	boss = shortcut;
	host = 0;
	widget = 0;
}
member_BShortcut::~member_BShortcut()
{

}

bool member_BShortcut::fromText(const BString& text)
{
	keys.clear();
	BString word;
	int pos = 0;
	while (pos < text.size())
	{
		pos = text.word(word, pos);
		if (word.empty())
			break;
		if (word == '+')
			continue;
		word.lower();
		if (Key key = StrToKey(word.cstr()))
		{
			keys.append(key);
		}
	}
	return keys.size();
}

